package com.meeting.meet.manager.impl.base;

import cn.hutool.core.collection.CollectionUtil;

import com.meeting.meet.manager.ReservationAndAgendaDtoManager;
import com.meeting.meet.manager.ReservationBaseDtoManager;
import com.meeting.meet.manager.converter.ReservationAndAgendaDtoConverter;
import com.meeting.meet.manager.converter.ReservationBaseDtoConverter;
import com.meeting.meet.manager.dto.ReservationAndAgendaDto;
import com.meeting.meet.manager.dto.ReservationBaseDto;
import com.meeting.meet.persist.dos.Reservation;
import com.meeting.meet.persist.mapper.ReservationDao;
import com.vs.code.AutoGenerated;
import com.vs.ox.common.exception.ErrorCode;
import com.vs.ox.common.exception.IgnoredException;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@AutoGenerated(locked = true, uuid = "07b8c8ea-1933-34bd-9f98-63b221515d4b")
public abstract class ReservationAndAgendaDtoManagerBaseImpl
        implements ReservationAndAgendaDtoManager {
    @AutoGenerated(locked = true)
    @Autowired
    private ReservationAndAgendaDtoConverter reservationAndAgendaDtoConverter;

    @AutoGenerated(locked = true)
    @Autowired
    private ReservationBaseDtoConverter reservationBaseDtoConverter;

    @AutoGenerated(locked = true)
    @Autowired
    private ReservationBaseDtoManager reservationBaseDtoManager;

    @AutoGenerated(locked = true)
    @Autowired
    private ReservationDao reservationDao;

    @AutoGenerated(locked = true, uuid = "23e2f5a7-4c15-3791-9025-33cfd38ae297")
    @Override
    public List<ReservationAndAgendaDto> getByRoomId(Long roomId) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        List<ReservationAndAgendaDto> reservationAndAgendaDtoList =
                getByRoomIds(Arrays.asList(roomId));
        return reservationAndAgendaDtoList;
        /** This block is generated by vs, do not modify, end anchor 1 */
    }

    @AutoGenerated(locked = true, uuid = "2ddf4a1f-aaaf-39f7-89ed-4839168257c3")
    @Override
    public List<ReservationAndAgendaDto> getByCreateUsers(List<Long> createUser) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        if (CollectionUtil.isEmpty(createUser)) {
            return Collections.emptyList();
        }

        List<Reservation> reservationList =
                reservationDao.getByCreateUsersForReservationAndAgendaDto(createUser);
        if (CollectionUtil.isEmpty(reservationList)) {
            return Collections.emptyList();
        }

        return doConvertFromReservationToReservationAndAgendaDto(reservationList);
        /** This block is generated by vs, do not modify, end anchor 1 */
    }

    @AutoGenerated(locked = true, uuid = "3729f6b1-596d-33a4-a99b-3fa39f04ec5e")
    @Override
    public List<ReservationAndAgendaDto> getByIds(List<Long> id) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        if (CollectionUtil.isEmpty(id)) {
            return Collections.emptyList();
        }

        List<Reservation> reservationList = reservationDao.getByIdsForReservationAndAgendaDto(id);
        if (CollectionUtil.isEmpty(reservationList)) {
            return Collections.emptyList();
        }

        // 结果和入参顺序保持一致
        Map<Long, Reservation> reservationMap =
                reservationList.stream().collect(Collectors.toMap(i -> i.getId(), i -> i));
        reservationList =
                id.stream()
                        .map(i -> reservationMap.get(i))
                        .filter(Objects::nonNull)
                        .collect(Collectors.toList());

        return doConvertFromReservationToReservationAndAgendaDto(reservationList);
        /** This block is generated by vs, do not modify, end anchor 1 */
    }

    @AutoGenerated(locked = true, uuid = "3cea81c9-18f9-3253-8c1d-be4659dd3ab4")
    @Override
    public ReservationAndAgendaDto getById(Long id) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        List<ReservationAndAgendaDto> ret = getByIds(Arrays.asList(id));
        if (ret.size() > 1) {
            throw new IgnoredException(ErrorCode.SYS_ERROR, "返回值个数超过1个");
        }
        ReservationAndAgendaDto reservationAndAgendaDto =
                CollectionUtil.isEmpty(ret) ? null : ret.get(0);
        return reservationAndAgendaDto;
        /** This block is generated by vs, do not modify, end anchor 1 */
    }

    @AutoGenerated(locked = true, uuid = "41cb1573-f261-3af0-8b61-aba8e49730b4")
    @Override
    public List<ReservationAndAgendaDto> getByCreateUser(Long createUser) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        List<ReservationAndAgendaDto> reservationAndAgendaDtoList =
                getByCreateUsers(Arrays.asList(createUser));
        return reservationAndAgendaDtoList;
        /** This block is generated by vs, do not modify, end anchor 1 */
    }

    @AutoGenerated(locked = true, uuid = "422170b8-f8d7-34ed-a0e4-405acd16cf7d")
    public List<ReservationAndAgendaDto> doConvertFromReservationToReservationAndAgendaDto(
            List<Reservation> reservationList) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        if (CollectionUtil.isEmpty(reservationList)) {
            return Collections.emptyList();
        }

        List<ReservationBaseDto> baseDtoList =
                reservationList.stream()
                        .map(
                                tmp ->
                                        reservationBaseDtoConverter
                                                .convertFromReservationToReservationBaseDto(tmp))
                        .collect(Collectors.toList());
        Map<Long, ReservationAndAgendaDto> dtoMap =
                reservationAndAgendaDtoConverter
                        .convertFromReservationBaseDtoToReservationAndAgendaDto(baseDtoList)
                        .stream()
                        .collect(
                                Collectors.toMap(
                                        ReservationAndAgendaDto::getId,
                                        Function.identity(),
                                        (o1, o2) -> o1));

        List<ReservationAndAgendaDto> reservationAndAgendaDtoList = new ArrayList<>();
        for (Reservation i : reservationList) {
            ReservationAndAgendaDto reservationAndAgendaDto = dtoMap.get(i.getId());
            if (reservationAndAgendaDto == null) {
                continue;
            }

            /** This block is generated by vs, do not modify, end anchor 1 */

            /** This block is generated by vs, do not modify, start anchor 2 */
            reservationAndAgendaDtoList.add(reservationAndAgendaDto);
        }
        return reservationAndAgendaDtoList;
        /** This block is generated by vs, do not modify, end anchor 2 */
    }

    @AutoGenerated(locked = true, uuid = "bfd5e2c5-4776-3150-9ef4-933ce766f95a")
    @Override
    public List<ReservationAndAgendaDto> getByRoomIds(List<Long> roomId) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        if (CollectionUtil.isEmpty(roomId)) {
            return Collections.emptyList();
        }

        List<Reservation> reservationList =
                reservationDao.getByRoomIdsForReservationAndAgendaDto(roomId);
        if (CollectionUtil.isEmpty(reservationList)) {
            return Collections.emptyList();
        }

        return doConvertFromReservationToReservationAndAgendaDto(reservationList);
        /** This block is generated by vs, do not modify, end anchor 1 */
    }
}
